package com.ruoyi.bussiness.service.impl;

import java.util.List;

import com.ruoyi.bussiness.domain.SysBusinessArtwork;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.bussiness.mapper.SysBusinessArtworkMapper;
import com.ruoyi.bussiness.domain.SysBusinessArtwork;
import com.ruoyi.bussiness.service.ISysBusinessArtworkService;

/**
 * 作品上传Service业务层处理
 *
 * @author ruoyi
 * @date 2024-01-23
 */
@Service
public class SysBusinessArtworkServiceImpl implements ISysBusinessArtworkService
{
    @Autowired
    private SysBusinessArtworkMapper sysBusinessArtworkMapper;

    /**
     * 查询作品上传
     *
     * @param id 作品上传主键
     * @return 作品上传
     */
    @Override
    public SysBusinessArtwork selectSysBusinessArtworkById(Long id)
    {
        return sysBusinessArtworkMapper.selectSysBusinessArtworkById(id);
    }

    /**
     * 查询作品上传列表
     *
     * @param sysBusinessArtwork 作品上传
     * @return 作品上传
     */
    @Override
    public List<SysBusinessArtwork> selectSysBusinessArtworkList(SysBusinessArtwork sysBusinessArtwork)
    {
        return sysBusinessArtworkMapper.selectSysBusinessArtworkList(sysBusinessArtwork);
    }

    /**
     * 新增作品上传
     *
     * @param sysBusinessArtwork 作品上传
     * @return 结果
     */
    @Override
    public int insertSysBusinessArtwork(SysBusinessArtwork sysBusinessArtwork)
    {
        sysBusinessArtwork.setCreateTime(DateUtils.getNowDate());
        sysBusinessArtwork.setIsBottom("0");
        sysBusinessArtwork.setIsTop("0");
        sysBusinessArtworkMapper.insertSysBusinessArtwork(sysBusinessArtwork);

        sysBusinessArtworkMapper.updateOrderNum(sysBusinessArtwork.getId());
        return 1;
    }

    /**
     * 修改作品上传
     *
     * @param sysBusinessArtwork 作品上传
     * @return 结果
     */
    @Override
    public int updateSysBusinessArtwork(SysBusinessArtwork sysBusinessArtwork)
    {
        sysBusinessArtwork.setUpdateTime(DateUtils.getNowDate());
        return sysBusinessArtworkMapper.updateSysBusinessArtwork(sysBusinessArtwork);
    }

    /**
     * 批量删除作品上传
     *
     * @param ids 需要删除的作品上传主键
     * @return 结果
     */
    @Override
    public int deleteSysBusinessArtworkByIds(Long[] ids)
    {
        return sysBusinessArtworkMapper.deleteSysBusinessArtworkByIds(ids);
    }

    /**
     * 删除作品上传信息
     *
     * @param id 作品上传主键
     * @return 结果
     */
    @Override
    public int deleteSysBusinessArtworkById(Long id)
    {
        return sysBusinessArtworkMapper.deleteSysBusinessArtworkById(id);
    }

    @Override
    public SysBusinessArtwork getLastOrNextArtwork(SysBusinessArtwork sysBusinessArtwork) {
        SysBusinessArtwork responseArtwork = null;
        if("last".equalsIgnoreCase(sysBusinessArtwork.getViewType())){
            responseArtwork = sysBusinessArtworkMapper.getLastArtwork(sysBusinessArtwork.getId(),sysBusinessArtwork.getPortfolioId());
        }else if("next".equalsIgnoreCase(sysBusinessArtwork.getViewType())){
            responseArtwork = sysBusinessArtworkMapper.getNextArtwork(sysBusinessArtwork.getId(),sysBusinessArtwork.getPortfolioId());
        }else {

        }
        return responseArtwork;
    }

    @Override
    public AjaxResult moveUpOrDown(SysBusinessArtwork entity) {
        SysBusinessArtwork moveEntity = sysBusinessArtworkMapper.selectSysBusinessArtworkById(entity.getId());
        if(moveEntity == null){
            return AjaxResult.error("作品不存在，不能上移下移");
        }
        SysBusinessArtwork moveChangeEntity = null;
        if("up".equalsIgnoreCase(entity.getMoveType())){
            moveChangeEntity = sysBusinessArtworkMapper.getLastList(moveEntity.getOrderNum());
            if(moveChangeEntity == null){
                return AjaxResult.error("首行数据不能上移");
            }
            Integer orderNum = moveEntity.getOrderNum();
            moveEntity.setOrderNum(moveChangeEntity.getOrderNum());
            moveChangeEntity.setOrderNum(orderNum);
            sysBusinessArtworkMapper.updateSysBusinessArtwork(moveEntity);
            sysBusinessArtworkMapper.updateSysBusinessArtwork(moveChangeEntity);
        }else if("down".equalsIgnoreCase(entity.getMoveType())){
            moveChangeEntity = sysBusinessArtworkMapper.getNextList(moveEntity.getOrderNum());
            if(moveChangeEntity == null){
                return AjaxResult.error("末行数据不能下移");
            }
            Integer orderNum = moveEntity.getOrderNum();
            moveEntity.setOrderNum(moveChangeEntity.getOrderNum());
            moveChangeEntity.setOrderNum(orderNum);
            sysBusinessArtworkMapper.updateSysBusinessArtwork(moveEntity);
            sysBusinessArtworkMapper.updateSysBusinessArtwork(moveChangeEntity);
        }else{
            return AjaxResult.error("无效的移动方式");
        }
        return AjaxResult.success("移动成功",1);
    }
}
